package com.itheima.health.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hantianzun
 * @since 2022/11/22 12:01
 */
@RequestMapping("/ordersetting")
@Slf4j
@RestController
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;
    /*
    * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
    * */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingBymonth(Integer year,Integer month){ //参数格式为：2022-11
        try {
            List<Map> list = orderSettingService.getOrderSettingBymonth(year,month);
            //获取预约设置数据成功
            return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }
    /*
    *Excel文件上传,并解析文件内容保存到数据库
    * */
   @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
       try {
           orderSettingService.editNumberByDate(orderSetting);
           //预约设置成功
           return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
       } catch (Exception e) {
           e.printStackTrace();
           //预约设置失败
           return new Result(false,MessageConst.ORDERSETTING_FAIL);
       }
   }
    /**
     * 上传预约设置的excel文件
     */
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) throws IOException {
        InputStream inputStream = multipartFile.getInputStream();
        Workbook workbook = new XSSFWorkbook(inputStream);
        List<OrderSetting> orderSettings = new ArrayList<>();
        for (Sheet sheet : workbook) {
            // 第0行为表头，直接从第1行取
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                //获取行
                Row row = sheet.getRow(i);
                //解析行
                OrderSetting orderSetting = parseOrderSetting(row);
                //加入列表
                orderSettings.add(orderSetting);
            }
        }
        // rpc调用数据入库
        orderSettingService.addAll(orderSettings);
        return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }

    private OrderSetting parseOrderSetting(Row row) {
        Date date = null;
        Integer number = null;
        try {
            //获取日期
            if (null != row.getCell(0)) {
                //如果单元格没有数据，则getDateCellValue返回null
                date = row.getCell(0).getDateCellValue();
            }
            //获取最大预约数量
            if (null != row.getCell(1)) {
                //如果单元格没有数据，则getNumericCellValue返回0
                double numberValue = row.getCell(1).getNumericCellValue();
                //将double型转为int型
                number = Double.valueOf(numberValue).intValue();
            }
        } catch (IllegalStateException e) {
            throw new BusinessRuntimeException(String.format("数据格式错误,%s 第%d行", row.getSheet().getSheetName(), row.getRowNum() + 1));
        }
        // 判断是否缺少数据
        if (null == date || null == number) {
            throw new BusinessRuntimeException(String.format("缺少必填数据,%s 第%d行", row.getSheet().getSheetName(), row.getRowNum() + 1));
        }
        return new OrderSetting(date, number);
    }


}